
/**
 * @description 客服系统
 * @AUTH along
 * @DATA 2020-09-23
 */
import MessageSocket from '@/assets/js/socket.js';
import { replaceEmoji } from '@/assets/js/emoji.js';

export default {
    namespaced: true,
    state: {
        login: false,//登录状态
        userInfo: {
            type: '',
            shop: {
                shop_no: '',
                shop_title: ''
            },
            nickname: '',
            head_img: '',
            phone: ''
        },//用户信息
        authList: [],//权限列表
        readyState: '',//socket连接状态
        socket: {},//连接参数
        session_no: '',//当前会话编号唯一
        chat_no: '',
        sessionList: [],//当前会话列表
        latelyList: [],//最近会话列表
        session_stat: {
            transfer_session_count: 0,
            current_session_count: 0,
            max_session_count: 0,
            queue_session_count: 0
        },//会话统计数量
        dataList: [],//当前会话记录
        msgReadUpdate: '',//标记信息已读更新
        msgList: [],//最新一条消息记录
        sessionResult: {},//转接通知
        tabObject: {},//当前左侧tab数据
        quitRow: {},//退出会话数据
        show: true,
        tab: 0,
        orderList: [],//订单列表
        order_change: '',//订单变动
        msg_retract_id: '',//消息撤回id
        is_check_msg: false,//是否查询留言时间
        messageNoticeId: ''
    },
    getters () {},
    mutations: {
        //设置登录状态
        SET_LOGIN (state, type) {
            state.login = type;
        },

        //设置用户信息
        SET_USER_INFO (state, info) {
            state.userInfo = {
                ...info
            };
        },

        //设置权限列表
        SET_AUTH_LIST (state, list) {
            state.authList = list;
        },

        //连接socket
        INIT_CONNECT_SOCKET (state, object) {
            let socket = new MessageSocket(this, {
                isHeartCheck: object.isHeartCheck,
                socketUrl: object.socketUrl,
                reConnectTime: object.reConnectTime
            });

            state.socket = {
                ...object
            };

            setTimeout(()=>{
                let readyState = socket.ws.readyState === WebSocket.OPEN;

                state.readyState = readyState;

                // console.log(readyState ? 'socket连接成功' : 'socket连接失败');
            },500);
        },

        //设置socket状态
        SET_READY_STATE (state, redyState) {
            state.readyState = redyState;
        },

        //断线重连
        RELINK_SOCKET (state ,object) {
            if(!state.readyState) {
                this.commit('global/INIT_CONNECT_SOCKET', object);
            }
        },

        //设置当前会话编号
        SET_SESSION (state, session_no) {
            sessionStorage.setItem('SESSION_NO', session_no);
            state.session_no = session_no;
        },

        //设置当前会话列表
        SET_SESSION_LIST (state, session_list) {
            state.sessionList = session_list;
        },

        //设置最近会话列表
        SET_SESSION_LATELY_LIST (state, session_list) {
            state.latelyList = session_list;
        },

        //设置会话统计数量
        SET_SESSION_STAT (state, session_stat) {
            state.session_stat = {
                ...session_stat
            };
        },

        //清空会话记录
        CLEAR_DATALIST (state) {
            state.dataList = [];
        },

        //更新数据
        UPDATE_LIST (state, list) {
            state.dataList = list;
        },

        //当前会话记录
        SET_DATALIST (state, list) {
            const data = JSON.parse(JSON.stringify(list));
            if(data.length == 1) {
                state.msgList = data;
                state.msgReadUpdate = data[0].msg_id + '&date=' + new Date().getTime();//生成唯一ID
            }

            const array = data.concat(state.dataList);
            // console.log('----收到的信息----', data);

            //按时间排序
            array.sort((a,b) => {
                return a.msg_time - b.msg_time;
            });

            //处理文本渲染
            for(let key=0; key<array.length; key++) {
                if(array[key].msg_type == 'text') {
                    array[key].msg_content = replaceEmoji(array[key].msg_content);
                }
            }

            // console.log('array', array);

            state.dataList = array;
        },

        //收到消息
        SET_DATALIST_TYPE (state, list) {
            const data = JSON.parse(JSON.stringify(list));

            // console.log('--data--', data, state.session_no == list[0].session_no);
            if(data.length == 1) {
                state.msgList = data;
                state.msgReadUpdate = data[0].msg_id + '&date=' + new Date().getTime();//生成唯一ID
            }

            // 是否同步socket消息
            if(state.session_no == list[0].session_no) {
                this.commit('global/SET_DATALIST', list);
            }
        },

        //发送消息
        ADD_MESSAGE (state, msg) {
            if(msg.msg_type == 'text') {
                msg.msg_content = replaceEmoji(msg.msg_content);
                state.dataList = state.dataList.concat([msg]);
            } else if (msg.msg_type == 'image') {
                state.dataList = state.dataList.concat([msg]);
            } else if (msg.msg_type == 'notice') {
                state.dataList = state.dataList.concat([msg]);
            }
        },

        // 设置全部已读
        SET_READ (state, data) {
            let dataList = state.dataList;
            let chatList = dataList.reduce((cur,next) => {
                cur.push(next.chat_no);
                return cur;
            },[]);

            if(chatList.includes(data.chat_no)) { //在当前会话中
                for(let i=0; i<dataList.length; i++) {
                    dataList[i].msg_read = 1;
                }
            }
            state.dataList = dataList;
        },

        //设置单条已读
        SET_READ_ONE (state, data) {
            let dataList = state.dataList;

            if(state.session_no == data.session_no) { //在当前会话中
                for(let i=0; i<dataList.length; i++) {
                    if(dataList[i].session_no == data.session_no) {
                        dataList[i].msg_read = 1;
                    }
                }
            }

            // console.log('dataList', dataList);

            state.dataList = dataList;
        },

        //设置tab
        SET_TAB (state, tab) {
            state.tab = tab;
        },

        //转接通知
        SET_SESSION_RESION (state, info) {
            state.sessionResult = info;
        },

        //设置当前会话数据
        SET_TAB_LIST (state, row) {
            state.tabObject = row;
        },

        //会话退出
        SESSION_QUIT (state, row) {
            state.quitRow = row;
        },

        //设置显示状态
        SET_SHOW (state, show) {
            state.show = show;
        },

        //排队变动
        QUEUE_CHANGE (state, count) {
            state.session_stat.queue_session_count = count.queue_count;
        },

        //设置聊天编号
        SET_CHAT (state, no) {
            console.log('no', no);
            state.chat_no = no;
        },

        //设置订单数据列表
        SET_ORDER_LIST(state,list){
            state.orderList = list;
        },

        // 监听订单变化
        SET_SHOP_ORDER_CREATE(state,value){
            state.order_change = value;
        },

        // 消息撤回
        SET_MSG_RETRACT(state,msg_id) {
            state.msg_retract_id = msg_id;
        },

        SET_MSG_TIME(state, type) {
            state.is_check_msg = type;
        },

        MESSAGE_NOTICE(state, id) {
            state.messageNoticeId = id;
        }
    },
    actions () {
        //
    }
};
